#region Copyright (c) 2007-2008, Chris Klochek

/********************************************************************************************************************
'
' Copyright (c) 2007-2008, Chris Klochek
' All rights reserved.
'
' Redistribution and use in source and binary forms, with or without modification, are permitted provided
' that the following conditions are met:
'
' * Redistributions of source code must retain the above copyright notice, this list of conditions and the
' 	following disclaimer.
'
' * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
' 	the following disclaimer in the documentation and/or other materials provided with the distribution.
'
' * Neither the name of the author nor the names of its contributors may be used to endorse or
' 	promote products derived from this software without specific prior written permission.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
' WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
' PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
' ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
' LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
' INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
' OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
' IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'
'*******************************************************************************************************************/

#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SuedeTest.FormsExplorer {
   public partial class FormExplorer : Form {

      /// <summary>
      /// Returns the name of the test entered in the FormExplorer's textbox.
      /// </summary>
      public string TestName {
         get {
            return txtTestName.Text;
         }
      }

      public FormExplorer() {
         InitializeComponent();

         windowFinder.ActiveWindowChanged += new EventHandler(windowFinder_ActiveWindowChanged);
         windowFinder.ActiveWindowSelected += new EventHandler(windowFinder_ActiveWindowSelected);
      }

      private void listViewExplorer_PropertyPathChanged(GridItem g, string propertyPath) {
         if (g != null) {
            _propertyPath = propertyPath;
         }
      }

      private void windowFinder_ActiveWindowSelected(object sender, EventArgs e) {
         object selectedObject = windowFinder.SelectedObject;

         ChangeSelectedObject(selectedObject);
      }

      internal IntPtr SelectedWindowHandle {
         get { return this.windowFinder.SelectedHandle; }
         set { this.windowFinder.SelectedHandle = value; }
      }

      void windowFinder_ActiveWindowChanged(object sender, EventArgs e) {
         object selectedObject = windowFinder.SelectedObject;

         ChangeSelectedObject(selectedObject);
      }

      //Selected object changed
      private bool ChangeSelectedObject(object selectedObject) {
         if (selectedObject != null) {
            grpControlExplorer.Visible = true;
            ShowControlExplorer();
            prpgControlProperties.SelectedObject = selectedObject;
            txtControlType.Text = selectedObject.ToString();

            return true;
         } else {
            grpControlExplorer.Visible = false;
            ShowToolPalette();
            return false;
         }
      }

      public delegate void ActionAddedHandler(SuedeTest.Core.Action action);
      public event ActionAddedHandler ActionAdded;

      public event EventHandler RecordPauseClicked;

      public event EventHandler ClearTestClicked;

      public event EventHandler SaveTestClicked;

      private string _propertyPath = "";
      private void prpgControlProperties_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e) {
         if (e.NewSelection != null) {
            txtControlName.Text = SuedeTest.Core.Util.ControlUtils.BuildUnambiguousName(prpgControlProperties.SelectedObject).TrimStart('.');
            if (e.NewSelection.Value is ListView.ListViewItemCollection) {
               //prpSubGrid.SelectedObject = e.NewSelection.Value;
            }
            _propertyPath = e.NewSelection.Label;
         }
      }

      public void OnActionAdded(SuedeTest.Core.Action a) {
         if (ActionAdded != null) {
            ActionAdded(a);
         }
      }

      private void btnAddAssert_Click(object sender, EventArgs e) {
         OnActionAdded(new SuedeTest.Core.PropertyAssertAction(txtControlName.Text + "+" + _propertyPath,
         prpgControlProperties.SelectedGridItem.Value.ToString()));
         ChangeSelectedObject(null);
      }

      private void btnAddDelay_Click(object sender, EventArgs e) {
         SuedeTest.Core.EventAction ea = new SuedeTest.Core.EventAction(null, "Wait", txtDelaySeconds.Text);
         OnActionAdded(ea);
      }

      private void FormExplorer_Activated(object sender, EventArgs e) {
         if (prpgControlProperties.SelectedObject != null) {
            ShowControlExplorer();
         } else {
            ShowToolPalette();
         }
      }

      private void FormExplorer_Deactivate(object sender, EventArgs e) {
         HideAll();
      }

      private void ShowControlExplorer() {
         this.Size = new Size(487, 295);
      }

      private void ShowToolPalette() {
         this.Size = new Size(223, 122);
      }

      private void HideAll() {
         this.Size = new Size(88, 63);
      }

      private void btnRecordPauseTest_Click(object sender, EventArgs e) {
         if (RecordPauseClicked != null) {
            RecordPauseClicked(sender, e);
         }
      }

      private void btnClearTest_Click(object sender, EventArgs e) {
         if (ClearTestClicked != null) {
            ClearTestClicked(sender, e);
         }
      }

      private void btnSaveTest_Click(object sender, EventArgs e) {
         if (SaveTestClicked != null) {
            SaveTestClicked(sender, e);
         }
      }

      private void btnHideControlExplorer_Click(object sender, EventArgs e) {
         ShowToolPalette();
      }
   }
}